package com.dexilaonian.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.adpter.SearchAdapter;
import com.dexilaonian.bean.ContentBean;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.JsonUtil;
import com.dexilaonian.utils.Okhttp;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class SearchActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.et_sousu_text3)
    EditText etSousuText3;
    @Bind(R.id.tv_store_search3)
    TextView tvStoreSearch3;
    @Bind(R.id.listview_search)
    ListView listviewSearch;
    private List<ContentBean> searchList = new ArrayList<>();

    @Override
    public int getLayoutID() {
        return R.layout.activity_search;
    }

    @Override
    public void initView() {

    }

    @Override
    protected void initData() {

    }
    private void initSearch() {
        final Drawable drawable = getResources().getDrawable(R.drawable.sousuo);
        // 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        etSousuText3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //  ivStoreSearch.setImageDrawable(getResources().getDrawable(R.drawable.weidiangouwuche));
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //ivStoreSearch.setImageDrawable(null);
                if (count != 0) {
                    etSousuText3.setCompoundDrawables(null, null, null, null);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    etSousuText3.setCompoundDrawables(drawable, null, null, null);
                    searchList.clear();
                    SearchAdapter adapter = new SearchAdapter(SearchActivity.this, searchList);
                    listviewSearch.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    //ivStoreSearch.setImageDrawable(getResources().getDrawable(R.drawable.weidiangouwuche));
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        etSousuText3.setFocusable(true);
        etSousuText3.setFocusableInTouchMode(true);
        etSousuText3.requestFocus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        initSearch();
    }

    @OnClick({R.id.back, R.id.tv_store_search3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_store_search3:
                String tvSearch = etSousuText3.getText().toString().trim();
                getSearch(tvSearch);
                break;
        }
    }
    private void getSearch(String tvSearch) {

        showProgressBar(true);
        String search_url = Constant.base_url + "/product/getProductsOfShop";
        Map<String, String> searchMap = new HashMap<>();
        searchMap.put("key", tvSearch);
        searchMap.put("index", 1 + "");
        searchMap.put("pageSize", 10 + "");
        Okhttp.postString(false, search_url, searchMap, new Okhttp.CallBac() {
            @Override
            public void onError(Call call, Exception e, String state, int id) {
                showProgressBar(false);
                showToast(state);
            }

            @Override
            public void onResponse(String response, int id) {
                showProgressBar(false);
                Log.e("TTTTsearch", response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 34 || status == 16) {
                        startActivity(new Intent(SearchActivity.this, LoginActivity.class));
                        return;
                    }
                    String data = jsonObject.optString("data");
                    JSONObject object = new JSONObject(data);
                    String items = object.optString("items");
                    List<ContentBean> contentBeen = JsonUtil.json2beans(items, ContentBean.class);

                    searchList.clear();
                    searchList.addAll(contentBeen);
                    SearchAdapter adapter = new SearchAdapter(SearchActivity.this, searchList);
                    listviewSearch.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {
                showProgressBar(false);
                showToast(state);
            }
        });
    }
}
